<?php
namespace Admin\Controller;

class DeliveryController extends BaseController  {

    public function index(){


        $this->display();
    }
    //快递单模板
    public function printer(){

        $priter = M('express_print_tmpl')->where(['local_id'=>session('userInfo')['local_id']])->select();

        $this->assign('priter',$priter);

        $this->display();
    }
    //发货信息管理
    public function deliveryInfo(){

        $list = M('express_dly_center')->where(['local_id'=>session('userInfo')['local_id']])->select();

        $this->assign('list',$list);
        $this->display('/Delivery/deliveryInfo');
    }

    //添加发货信息管理
    public function deliveryInfoAdd(){
        $data = $_POST;

        if($data['dly_center_id']!=''){
            $data['local_id']= session('userInfo')['local_id'];
            //编辑
            if(empty($data['name'])){
                $this->error('请填写发货点名称');
            }
            if(empty($data['region'])){
                $this->error('请填写地区');
            }
            if(empty($data['address'])){
                $this->error('请填写详细地址');
            }
            if(!empty($data['disabled'])){
                $data['disabled'] = 'true';
            }else{
                $data['disabled'] = 'false';
            }


            $res = M('express_dly_center')->save($data);

            if($res !==false){

                if($data['disabled']==='true'){
                    M('express_dly_center')->where(['local_id'=>session('userInfo')['local_id']])->save(array('disabled'=>'false'));
                    M('express_dly_center')->where(['dly_center_id'=>$data['dly_center_id'],'local_id'=>session('userInfo')['local_id']])->save(array('disabled'=>'true'));
                }
                $this->success('修改成功', '/Admin/Delivery/deliveryInfo.html');
                exit;
            }else{
                $this->success('修改失败', '/Admin/Delivery/deliveryInfo.html');
                exit;
            }

        }else{
            //增加
            if (!empty($data)) {

                $data['local_id']= session('userInfo')['local_id'];
                if(empty($data['name'])){
                    $this->error('请填写发货点名称');
                }
                if(empty($data['region'])){
                    $this->error('请填写地区');
                }
                if(empty($data['address'])){
                    $this->error('请填写详细地址');
                }

                $res = M('express_dly_center')->data($data)->add();


                if($res){

                    if(!empty($data['disabled'])){
                        M('express_dly_center')->where(['local_id'=>session('userInfo')['local_id']])->save(array('disabled'=>'false'));
                        M('express_dly_center')->where(['dly_center_id'=>$res,'local_id'=>session('userInfo')['local_id']])->save(array('disabled'=>'true'));
                    }else{
                        $data['is_default']='false';
                    }


                    $this->success('添加成功', '/Admin/Delivery/deliveryInfo.html');
                    exit;
                }else{
                    $this->success('添加失败', '/Admin/Delivery/deliveryInfo.html');
                    exit;
                }

            }

        }

        $this->display('/Delivery/addDliveryInfo');
    }
    //添加发货信息管理
    public function deliveryInfoDelete(){
        $id = $_POST['id'];
        $res = M('express_dly_center')->where(['dly_center_id'=>$id,'local_id'=>session('userInfo')['local_id']])->delete();
        if($res){
            $this->ajaxReturn(array('msg'=>'删除成功！','status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>'删除失败！','status'=>0));
        }
    }

    public function deliveryInfoEdit(){
        $id = I('id' , '', htmlentities());
        $info = M('express_dly_center')->where(['dly_center_id'=>$id,'local_id'=>session('userInfo')['local_id']])->find();

        $this->assign('info',$info);
        $this->display('/Delivery/addDliveryInfo');

    }
    //快递单模板
    public function Delete(){
        $id = $_POST['id'];
        $res = M('express_print_tmpl')->where(['prt_tmpl_id'=>$id,'local_id'=>session('userInfo')['local_id']])->delete();
        if($res){
            $this->ajaxReturn(array('msg'=>'删除成功！','status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>'删除失败！','status'=>0));
        }
    }


    //快递单模板编辑
    public function Edit(){
        $id = I('id' , '', htmlentities());
        $Einfo = M('express_print_tmpl')->where(['prt_tmpl_id'=>$id,'local_id'=>session('userInfo')['local_id']])->find();

        $this->assign('editInfo',$Einfo);
        $this->display('/Delivery/editPriter');
    }
    //快递单模板
    public function Add(){

        $id = I('id' , '', htmlentities());

        $data['shop_name'] = '店铺名称'; //网店名称
        $data['ship_name'] = '收货人姓名';; //收货人姓名
        $data['ship_area'] = '收货人地区'; //收货人地区
        $data['ship_addr'] = '收货人地址'; //收货人地址
       // $data['ship_tel'] = '收货人电话';; //收货人电话
        $data['ship_mobile'] = '收货人手机';; //收货人手机
        //$data['ship_zip'] = '收货人邮编'; //收货人邮编
        $data['ship_detail_addr'] = '收货人-地区+详细地址'; //收货人-地区+详细地址


        $data['dly_name'] = '发货人姓名'; //发货人姓名
        $data['dly_area'] = '发货人地区'; //发货人地区
        $data['dly_address'] = '发货人地址'; //发货人地址
        $data['dly_tel'] = '发货人电话'; //发货人电话
        $data['dly_mobile'] = '发货人手机'; //发货人手机
        $data['dly_zip'] = '发货人邮编'; //发货人邮编

        $data['date_y'] = '当日日期-年'; //当日日期-年
        $data['date_m'] = '当日日期-月'; //当日日期-月
        $data['date_d'] = '当日日期-日'; //当日日期-日

        $data['order_id'] = '订单-订单号'; //订单-订单号
        $data['order_price'] = '订单总金额'; //订单总金额
        $data['order_weight'] = '订单物品总重量'; //订单物品总重量
        $data['order_count'] = '订单-物品数量'; //订单-物品数量
        $data['order_memo'] = '订单-备注'; //订单-备注
        $data['ship_time'] = '订单-送货时间'; //订单-送货时间

        $data['tick']= '√'; //对号

        $data['member_name'] = '会员用户名'; //会员用户名
        $data['order_name'] = '订单商品名称'; //订单商品名称
        $data['order_name_a'] = '订单商品名称+数量'; //订单商品名称+数量
        $data['order_name_as']='订单商品名称+规格+数量'; //订单商品名称+规格+数量
        $data['order_name_ab'] = '订单商品名称+货号+数量'; //订单商品名称+货号+数量
        //模板选项参数 预览初始化值
        if(!empty($id)){
           $ss = M('express_print_tmpl')->where(['prt_tmpl_id'=>$id,'local_id'=>session('userInfo')['local_id']])->find();
           $ss['prt_tmpl_data'] =  str_replace('px','',$ss['prt_tmpl_data']);
           $drt =  json_decode($ss['prt_tmpl_data'],true);
            foreach ($drt as $k=>&$value){
                 $value['family'] = str_replace('\'','',$value['family']);
            }
            $ss['prt_tmpl_data'] =json_encode($drt);

        }
       // print_r($ss);
        //自定义内容 预览传递过来
        $this->assign('ss',$ss);
        $this->assign('data',$data);
        $this->display('/Delivery/dlytmplAdd');
    }



    //保存打印订单模板
    public function savePrint(){
        $content = $_POST['data'];

        $data['prt_tmpl_data'] =json_encode($content['info']);
        $data['prt_tmpl_width'] =$content['wrap']['width'];
        $data['prt_tmpl_height'] =$content['wrap']['height'];
        $data['prt_tmpl_id'] =$content['wrap']['prt_tmpl_id'];
        $data['prt_tmpl_title'] =$content['wrap']['prt_tmpl_title'];
        $data['pic_url'] =$content['wrap']['pic_url'];
        $data['local_id'] = session('userInfo')['local_id'];

        if($content['wrap']['shortcut'] == 'on'){
            $data['shortcut'] = 'false';
        }else{
            $data['shortcut'] =$content['wrap']['shortcut'];
        }
        if(empty($data['prt_tmpl_title'])){
            $this->ajaxReturn(array('msg'=>'请填写快递单模板名称'));
        }
        if($data['prt_tmpl_data']==='null'){
            $this->ajaxReturn(array('msg'=>'快递单信息不能为空'));
        }
        if(empty($data['prt_tmpl_title'])){
            $this->ajaxReturn(array('msg'=>'请填写快递单模板名称'));
        }

        if($data['prt_tmpl_id']!=''){
            $res = M('express_print_tmpl')->save($data);
        }else{
            $res = M('express_print_tmpl')->add($data);
        }
        if($res){
            $this->ajaxReturn(array('msg'=>'操作成功','status'=>1));
        }else{
            $this->ajaxReturn(array('msg'=>'操作失败','status'=>0));
        }

    }

}